package com.example.todolist.adapter;

import java.text.SimpleDateFormat;
import java.util.List;

import com.example.todolist.R;
import com.example.todolist.entity.ToDoItem;
import com.example.todolist.views.ToDoListItemView;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ToDoItemAdapter extends ArrayAdapter<ToDoItem> {
	
	private int resource;

	public ToDoItemAdapter(Context context, int resourceId,
			List<ToDoItem> objects) {
		super(context, resourceId, objects);

		this.resource = resourceId;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LinearLayout toDoView;
		if(convertView == null) {
			toDoView = new LinearLayout(getContext());
			LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			inflater.inflate(resource, toDoView, true);
		} else {
			toDoView = (LinearLayout) convertView;
		}
		
		ToDoItem toDoItem = getItem(position);
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy");
		String dateString = sdf.format(toDoItem.getCreated());
		
		ToDoListItemView task = (ToDoListItemView) toDoView.findViewById(R.id.row);
		TextView created = (TextView) toDoView.findViewById(R.id.rowDate);
		
		task.setText(toDoItem.getTask());
		created.setText(dateString);
		
		return toDoView;
	}

}
